﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using DAL;
using System.IO;

namespace WebSiteAlex
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetExportation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            String reportId = context.Request.QueryString["id"];
            String type = context.Request.QueryString["type"];
            if (!String.IsNullOrEmpty(reportId) && !String.IsNullOrEmpty(type))
            {
                Guid rId = new Guid(reportId);
                ExpenseExportation ee = ExpenseExportation.Get(rId, type);
                FileInfo file = new FileInfo(ee.Path);
                if (file.Exists)
                {
                    context.Response.Clear();
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + ee.ExpenseReport.Name+".xls");
                    context.Response.AddHeader("Content-Length", file.Length.ToString());
                    context.Response.WriteFile(file.FullName);
                }
            }           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
